package com.cqut.forum.dao.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.cqut.forum.dao.ModuleDao;

@Component
public class ModuleDaoImp extends BaseDaoImp implements ModuleDao{
	
	public <T> boolean unDelete(int id) {
		return false;
	}
	@SuppressWarnings("unchecked")
	public int getModuleIndex(String modleName){
		String hql = "select moduleId from Module where moduleName = ? and moduleState=true";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(modleName);
		List<Integer> module = (List<Integer>) super.excuteHql(hql, parameters, null);
		
		return module.size()>0?(int)module.get(0):0;
	}

	@Override
	public boolean updateModuleCss(Integer moduleId, String css) {
		String sql = "update module_information mi,module m set mi.module_css=? where " +
				"m.module_information_id=mi.module_information_id and m.module_id=?";
		Query query = super.getSqlQuery(sql);
		query.setString(0, css);
		query.setInteger(1, moduleId);
		query.executeUpdate();
		return true;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> loadModules() {
		String sql ="select mi.module_css,mi.information_name,m.module_id,m.module_name,m.module_state " +
				"from module m,module_information mi where m.module_information_id=mi.module_information_id " +
				"and m.module_state= true";
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, null, null);
		return result;
	}
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> loadModuleInfo(String moduleName) {
		String sql ="select mi.module_css,mi.information_name,m.module_id,m.module_name,m.module_state " +
			"from module m,module_information mi where m.module_information_id=mi.module_information_id " +
			"and m.module_state= true and m.moduleName=?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(moduleName);
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, null);
		return result.size()>0?result.get(0):null;
	}
}
